package com.atguigu.controller;


import com.atguigu.bean.idName;
import com.atguigu.client.OrderFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.client.SecKillFeignClient;
import com.atguigu.client.WareFeignClient;
import com.atguigu.constant.MqConst;
import com.atguigu.entity.*;
import com.atguigu.result.RetVal;
import com.atguigu.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestOperations;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库存单元表 前端控制器
 * </p>
 *
 * @author yangchaolove
 * @since 2021-11-19
 */
@RestController
@RequestMapping("/product")
public class SkuController {
    @Autowired
    private ProductSalePropertyKeyService salePropertyKeyService;
    @Autowired
    private ProductImageService productImageService;
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private SearchFeignClient searchFeignClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private BaseBrandService brandService;
    @Autowired
    private SecKillFeignClient secKillFeignClient;
    @Autowired
    private WareInfoService wareInfoService;
    @Autowired
    private WareFeignClient wareFeignClient;

    @Autowired
    private OrderFeignClient orderFeignClient;
      //01 根据分类ID查询平台属性 (已经完成)


     // 02 根据spu id查询 销售属性 （key value）
     @GetMapping("querySalePropertyByProductId/{productId}")
     public RetVal querySalePropertyByProductId(@PathVariable Long productId){
         List<ProductSalePropertyKey> salePropertyList= salePropertyKeyService.querySalePropertyByProductId(productId);
         return RetVal.ok(salePropertyList);
     }

    // 03 根据spu id 查询图片信息 http://127.0.0.1/product/queryProductImageByProductId/12
    @GetMapping("queryProductImageByProductId/{productId}")
    public RetVal queryProductImageByProductId(@PathVariable Long productId){
        QueryWrapper<ProductImage> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",productId);
        List<ProductImage> productImageList = productImageService.list(wrapper);
        return RetVal.ok(productImageList);
    }
    @PostMapping("saveSkuInfo")
    public RetVal saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuInfoService.saveSkuInfo(skuInfo);
        return RetVal.ok();
    }

    //分页查询skuinfo
    @GetMapping("querySkuInfoByPage/{pageNum}/{pageSize}")
    public RetVal querySkuInfoByPage(@PathVariable Long pageNum, @PathVariable Long pageSize) {
        Page<SkuInfo> skuInfoPage = new Page<>(pageNum, pageSize);
        skuInfoService.querySkuInfoByPage(skuInfoPage);
        return RetVal.ok(skuInfoPage);
    }
    //5.商品上架
    @GetMapping("onSale/{skuId}")
    public RetVal onSale(@PathVariable Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(1);
        skuInfoService.updateById(skuInfo);
//        searchFeignClient.onSale(skuId);  //创建
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.ON_SALE_ROUTING_KEY,skuId);  //on.off.exchange  on.sale.routing.key  skuid
        return RetVal.ok();
    }
    //6.商品下架
    @GetMapping("offSale/{skuId}")
    public RetVal offSale(@PathVariable Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(0);
        skuInfoService.updateById(skuInfo);
//        searchFeignClient.offSale(skuId);  //删除
        rabbitTemplate.convertAndSend(MqConst.ON_OFF_SALE_EXCHANGE,MqConst.OFF_SALE_ROUTING_KEY,skuId);  //  on.off.exchange off.sale.routing.key
        return RetVal.ok();
    }

    //7 查询skuinfo id name
    @GetMapping("getSkuInfoForView")
    public List<idName> getSkuInfoForView() {
        List<idName> skulist=skuInfoService.getAll();
        return skulist;
    }
    //8 查询skuinfo
    @GetMapping("getSkuInfo")
    public RetVal getSkuInfo() {

        List<idName> skulist=skuInfoService.getAll();
        return RetVal.ok(skulist);
    }
    //在售商品信息
    @GetMapping("/querySeckillByPage/{currentPageNum}/{pageSize}")
    public RetVal querySeckillByPage(
                                     @PathVariable Long currentPageNum,
                                     @PathVariable Long pageSize){
        //分页对象
        RetVal<List<Long>> retVal = secKillFeignClient.findAll();

        Page<SkuInfo> page = new Page<>(currentPageNum, pageSize);
        QueryWrapper<SkuInfo> wrapper= new  QueryWrapper<>();
        wrapper.eq("is_sale",1);
        wrapper.notIn("id",retVal.getData().toArray());
//        List<SkuInfo> list = skuInfoService.list(wrapper);
        skuInfoService.page(page,wrapper);
        page.getRecords().forEach(e->{
            BaseBrand brand = brandService.getById(e.getBrandId());
//            BeanUtils.copyProperties(brand,e);
            e.setBrandId(brand.getId());
            e.setBrandName(brand.getBrandName());
            e.setBrandLogoUrl(brand.getBrandLogoUrl());
        });
        return RetVal.ok(page);
    }

    //8 查询仓库信息集合
    @GetMapping("getWareInfoList")
    public RetVal getWareInfoList() {
        List<WareInfo> list = wareInfoService.listWare();
        return RetVal.ok(list);
    }

    //9 新增
    @PostMapping("saveWare")
    public RetVal saveWare(@RequestBody WareInfo wareInfo) {
         WareInfo wareInfo1= wareInfo;
         wareInfoService.save(wareInfo1);
         wareFeignClient.saveWare(wareInfo1);
         return RetVal.ok();
    }

    //9 修改
    @PostMapping("updateWare")
    public RetVal updateWare(@RequestBody WareInfo wareInfo) {
        wareInfoService.updateById(wareInfo);
        wareFeignClient.updateWare(wareInfo);
        return RetVal.ok();
    }

    //10 删除
    @DeleteMapping("deteleWare")
    public RetVal deteleWare(@RequestBody WareInfo wareInfo) {
        wareInfoService.removeById(wareInfo.getId());
        wareFeignClient.deteleWare(wareInfo);
        return RetVal.ok();
    }

//    ====================库存
    //8 查询仓库信息集合
    @GetMapping("WareList")
    public RetVal WareList() {
        List<WareSku> wareSkuList = wareFeignClient.WareSkuList();
        wareSkuList.forEach(e->{
            e.setRemaining(e.getStock()-e.getStockLocked());
        });
        return RetVal.ok(wareSkuList);
    }

    //9
    @PostMapping("updateWareSku")
    public RetVal updateWareSku(@RequestBody Map<String, Object> map) {
         wareFeignClient.updateWareSku(map);
        return RetVal.ok();
    }
    //10  查询订单信息
    @GetMapping("Backstage/{pageNo}/{pageSize}")
    public RetVal Backstage(@PathVariable Integer pageNo,@PathVariable Integer pageSize) {
        return RetVal.ok(orderFeignClient.Backstage(pageNo, pageSize));
    }
}

